<template>
  <div>
    <a-tabs v-model:activeKey="activeKey" type="card" @change="tabChange">
      <a-tab-pane key="tab1" tab="系统设置">
        <a-form
          ref="FormRef"
          :model="FormState"
          :rules="FormRule"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
        >
          <a-form-item label="IP白名单限制" has-feedback name="ip_limit">
            <a-radio-group v-model:value="FormState.ip_limit" name="radioGroup">
              <a-radio :value="0">不开启</a-radio>
              <a-radio :value="1">开启</a-radio>
            </a-radio-group>
          </a-form-item>
          <a-form-item
            v-if="FormState.ip_limit == 1"
            label="IP白名单"
            has-feedback
            name="server_ip"
          >
            <a-select
              v-model:value="FormState.server_ip"
              mode="tags"
              style="width: 100%"
              :token-separators="[',']"
              placeholder="请输入可调用短信接口的IP白名单，多个以,分割"
            />
          </a-form-item>
          <a-form-item style="margin-left: 12.5%">
            <a-button type="primary" html-type="submit" :loading="ConfirmLoading" @click="OnSubmit">
              提交
            </a-button>
          </a-form-item>
        </a-form>
      </a-tab-pane>
      <a-tab-pane key="tab2" tab="频率设置">
        <a-alert
          message="针对同短信配置同手机号的发送频率进行限制(注：填写值为0时为不限制)"
          type="warning"
          show-icon
          style="margin-bottom: 20px"
        />
        <a-form
          ref="FormRef"
          :model="FormState"
          :rules="FormRule"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
        >
          <a-form-item label="每分钟内发送上限" has-feedback name="minute_limit">
            <a-input-number
              v-model:value="FormState.minute_limit"
              placeholder="请填写"
              addon-after="条"
            />
          </a-form-item>
          <a-form-item label="每小时内发送上限" has-feedback name="hour_limit">
            <a-input-number
              v-model:value="FormState.hour_limit"
              placeholder="请填写"
              addon-after="条"
            />
          </a-form-item>
          <a-form-item label="每个自然日内发送上限" has-feedback name="day_limit">
            <a-input-number
              v-model:value="FormState.day_limit"
              placeholder="请填写"
              addon-after="条"
            />
          </a-form-item>
          <a-form-item style="margin-left: 12.5%">
            <a-button type="primary" html-type="submit" :loading="ConfirmLoading" @click="OnSubmit">
              提交
            </a-button>
          </a-form-item>
        </a-form>
      </a-tab-pane>
    </a-tabs>
  </div>
</template>

<script lang="ts">
import { defineComponent, reactive, toRefs, onMounted, ref, toRaw } from 'vue'
import { Row, Col, Tabs, InputNumber } from 'ant-design-vue'
import { UploadOutlined, PaperClipOutlined } from '@ant-design/icons-vue'
import { requestSuccess, requestFailed } from '@/utils/feedback'
import { getSendSetting, editSendSetting } from '@/api/sms'

export default defineComponent({
  name: 'SendSetting',
  components: {
    UploadOutlined,
    PaperClipOutlined,
    [Row.name]: Row,
    [InputNumber.name]: InputNumber,
    [Tabs.name]: Tabs,
    [Col.name]: Col
  },
  setup() {
    const FormRef = ref()
    const state = reactive({
      ConfirmLoading: false,
      roleList: [],
      activeKey: ref('tab1'),
      labelCol: {
        xs: { span: 24 },
        sm: { span: 3 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 20 }
      }
    })
    const FormState = reactive({
      ip_limit: '',
      server_ip: '',
      minute_limit: '',
      hour_limit: '',
      day_limit: ''
    })

    const FormRule = {
      wechat_appid: [
        { required: false, message: '请填写微信登录APPID', trigger: ['blur', 'change'] }
      ],
      wechat_secret: [
        { required: false, message: '请填写微信登录Secret', trigger: ['blur', 'change'] }
      ]
    }
    const FormRule2 = {
      dragon_secret_key: [
        { required: false, message: '请填写密钥Key', trigger: ['blur', 'change'] }
      ],
      dragon_captcha_id: [{ required: false, message: '请填写', trigger: ['blur', 'change'] }]
    }
    onMounted(() => {
      getDataList()
    })

    const tabChange = () => {
      getDataList()
    }

    function getDataList() {
      getSendSetting({ tab: state.activeKey }).then((res) => {
        for (let item in res) {
          FormState[item] = res[item]
        }
      })
    }
    const OnSubmit = () => {
      FormRef.value
        .validate()
        .then(() => {
          editSendSetting(toRaw(FormState))
            .then((res) => {
              if (res.code === 200) {
                requestSuccess(res)
              } else {
                requestFailed(res)
              }
            })
            .finally(() => {
              state.ConfirmLoading = false
            })
        })
        .catch(() => {
          return false
        })
    }
    return {
      ...toRefs(state),
      FormRef,
      FormState,
      FormRule,
      FormRule2,
      tabChange,
      OnSubmit,
      getDataList
    }
  }
})
</script>

<style lang="scss" scoped></style>
